from moviepy.editor import VideoFileClip, concatenate_videoclips, TextClip, CompositeVideoClip

def basic_video_editing():
    # 加载视频文件
    video = VideoFileClip("20250914-demo.mp4")
    
    # 1. 视频裁剪 - 截取前10秒
    clipped_video = video.subclip(668, 788)
    
    # 2. 调整视频大小
    # resized_video = clipped_video.resize(width=840)  # 保持比例调整宽度为640
    resized_video = clipped_video
    
    # 3. 添加文字水印
    #text = TextClip("lanxum.com", fontsize=60, color='white')
    #text = text.set_position(('center', 'bottom')).set_duration(10)
    
    # 合并视频和文字
    #video_with_text = CompositeVideoClip([resized_video, text])
    video_with_text = resized_video
    
    # 4. 提取音频
    # audio = video_with_text.audio
    # audio.write_audiofile("output_audio.mp3")
    # 获取原始视频的帧率
    print(f"原始视频帧率: {video.fps}")
    # 确认合成视频的帧率
    print(f"合成视频帧率: {video_with_text.fps}")  # 确保输出为30

    # 5. 保存处理后的视频 - 明确指定帧率
    video_with_text.write_videofile(
        "output_video.mp4",
        codec="libx264",
        audio_codec="aac",
        bitrate="5000k",
        fps=video.fps  # 使用已确认有效的帧率
    )
    
    # 关闭视频文件释放资源
    video.close()
    clipped_video.close()
    resized_video.close()
    video_with_text.close()

def concatenate_videos():
    # 合并多个视频片段
    clip1 = VideoFileClip("clip1.mp4").subclip(0, 5)
    clip2 = VideoFileClip("clip2.mp4").subclip(2, 7)
    
    # 确保两个视频尺寸相同
    clip2 = clip2.resize(clip1.size)
    
    # 合并视频
    final_clip = concatenate_videoclips([clip1, clip2])
    final_clip.write_videofile("merged_video.mp4")
    
    # 释放资源
    clip1.close()
    clip2.close()
    final_clip.close()

if __name__ == "__main__":
    # 基本视频编辑
    basic_video_editing()
    
    # 合并视频示例
    # concatenate_videos()


